package cn.sccl.common.scaffold;

import cn.sccl.common.util.StrUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.JdbcType;

import java.math.BigDecimal;

/**
 * Created by 杨浩宇 on 2016-02-20.
 */
public class ColumnInfo {
    private String columnName;

    private String orgJdbcType;

//    private String jdbcType;

    private int size;

    private boolean nullable;

    private String comments;

    private int digits;

    private boolean inBaseModel;

//    private String propertyName;
//
//    private String javaType;

    public static Class<?> toJavaType(String jdbcType, int digit, int size) {
        jdbcType = StringUtils.upperCase(jdbcType);
        Class result = Long.class;
        if (jdbcType.contains("CHAR")) {
            result = String.class;
        }
        if (jdbcType.contains("CLOB")) {
            result = String.class;
        }
        if (jdbcType.contains("BLOB")) {
            result = byte.class;
        }
        if (jdbcType.contains("LONGBLOB"))
        {
            result = byte.class;
        }
        if (jdbcType.contains("DATE")) {
            result = java.util.Date.class;
        }
        if (jdbcType.contains("TIMESTAMP")) {
            result = java.sql.Timestamp.class;
        }
        if (jdbcType.contains("NUMBER") && digit > 0) {// 有小数精度
            result = java.math.BigDecimal.class;
        } else if (jdbcType.contains("NUMBER") && size == 1) {// 有小数精度
            result = boolean.class;
        }
        if (jdbcType.contains("NUMERIC") && digit > 0) {// 有小数精度
            result = java.math.BigDecimal.class;
        } else if (jdbcType.contains("NUMERIC") && size == 1) {// 有小数精度
            result = boolean.class;
        }
        if (jdbcType.contains("INT") && size ==1){
            result = boolean.class;
        }else if(jdbcType.contains("INT") && size > 10){
            result = Long.class;
        }


        return result;
    }

    public static String toPropertyName(String columnName) {
        return StrUtils.toCamelCase(columnName.toLowerCase());
    }

//    public String getJdbcType() {
//        return jdbcType;
//    }

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments == null ? "" : comments;
        ;
    }

    public boolean isNullable() {
        return nullable;
    }

    public void setNullable(boolean nullable) {
        this.nullable = nullable;
    }

    public int getDigits() {
        return digits;
    }

    public void setDigits(int digits) {
        this.digits = digits;
    }

    public String getOrgJdbcType() {
        return orgJdbcType;
    }

    public void setOrgJdbcType(String orgJdbcType) {
        this.orgJdbcType = orgJdbcType;
//        this.jdbcType = toMybatisJdbcType(orgJdbcType);
    }

    public boolean isInBaseModel() {
        return inBaseModel;
    }

    public void setInBaseModel(boolean inBaseModel) {
        this.inBaseModel = inBaseModel;
    }

    public String getPropertyName() {
//        return propertyName;
        return toPropertyName(getColumnName());
    }

    public String getJavaType() {
//        return javaType;
        return toJavaType(getOrgJdbcType(), getDigits(), getSize()).getName();
    }

    public String getJdbcType() {
        String jdbcType = "";
        if (orgJdbcType.contains(JdbcType.VARCHAR.name())) {
            jdbcType = JdbcType.VARCHAR.name();
        } else if (orgJdbcType.contains(JdbcType.CHAR.name())) {
            jdbcType = JdbcType.CHAR.name();
        } else if (orgJdbcType.contains("CLOB")) {
            jdbcType = JdbcType.CLOB.name();
        } else if (orgJdbcType.contains("BLOB")) {
            jdbcType = JdbcType.BLOB.name();
        } else if (orgJdbcType.contains(JdbcType.DATE.name())) {
            jdbcType = JdbcType.DATE.name();
        } else if (orgJdbcType.contains(JdbcType.TIMESTAMP.name())) {
            jdbcType = JdbcType.TIMESTAMP.name();
        } else if (orgJdbcType.contains(JdbcType.TIME.name())) {
            jdbcType = JdbcType.TIME.name();
        } else if (orgJdbcType.contains("NUMBER")) {
            jdbcType = JdbcType.NUMERIC.name();
        }
        return jdbcType;
    }

}
